package cn.gz.demo.ui.calendar;


import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.haibin.calendarview.CalendarView;
import com.haibin.calendarview.TrunkBranchAnnals;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.gz.demo.CalendarBinding;
import cn.gz.demo.R;
import cn.gz.demo.ui.base.BaseActivity;
import cn.gz.demo.ui.calendar.custom.CustomMonthView;
import cn.gz.demo.ui.calendar.custom.CustomWeekBar;
import cn.gz.demo.ui.calendar.custom.CustomWeekView;
import cn.gz.demo.utils.LogUtil;

/**
 * @author Liuyz
 * @date 2020/6/1514:45
 */
public class CalendarActivity extends BaseActivity {
    private CalendarBinding binding;
    private Calendar calendar;
    private int mYear;
    private int year, month, day, lastDay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_calendar);

        initView();
        initListener();
        initData();
    }

    private void initView() {

        calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH) + 1;
        day = calendar.get(Calendar.DAY_OF_MONTH);
        lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        LogUtil.showLog("year.." + year + "month.." + month + "day.." + day + "lastDay.." + lastDay);

        mYear = year;
        binding.day.setText(year + "年" + month + "月");
        binding.calendarView.setRange(2018, 1, 1, year, month, lastDay);

        binding.calendarView.setMonthView(CustomMonthView.class); //设置背景
        binding.calendarView.setWeekBar(CustomWeekBar.class);
        binding.calendarView.setWeekView(CustomWeekView.class);

        binding.calendarView.setSchemeColor(0xFFff0000, 0xFFffffff, 0xFFffffff);
    }

    private void initListener() {
        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        binding.reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.calendarView.scrollToCurrent(true);
            }
        });
        binding.lastMonth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.calendarView.scrollToPre(false);
            }
        });
        binding.nextMonth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.calendarView.scrollToNext(false);
            }
        });
        binding.day.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!binding.calendarLayout.isExpand()) {
                    binding.calendarLayout.expand();
                    return;
                }
                binding.calendarView.showYearSelectLayout(mYear);
            }
        });
        // 年份改变事件
        binding.calendarView.setOnYearChangeListener(new CalendarView.OnYearChangeListener() {
            @Override
            public void onYearChange(int year) {
                binding.day.setText(String.valueOf(year));
                LogUtil.showLog("onYearChange 年份变化 " + year);
            }
        });
        // 日期选择事件
        binding.calendarView.setOnCalendarSelectListener(new CalendarView.OnCalendarSelectListener() {
            @Override
            public void onCalendarOutOfRange(com.haibin.calendarview.Calendar calendar) {
                LogUtil.showLog(String.format("%s : OutOfRange", calendar));
            }

            @Override
            public void onCalendarSelect(com.haibin.calendarview.Calendar calendar, boolean isClick) {
                binding.day.setText(String.valueOf(calendar.getYear()) + "年" + calendar.getMonth() + "月");
                mYear = calendar.getYear();
                LogUtil.showLog("onCalendarSelect  -- " + getCalendarText(calendar));
                LogUtil.showLog("onCalendarSelect  -- " + calendar.getYear() + "  --  " + calendar.getMonth() + "  -- " + calendar.getDay() + "  --  " + isClick + "  --   " + calendar.getScheme());
                LogUtil.showLog("onCalendarSelect  " + binding.calendarView.getSelectedCalendar().getScheme() + "  --  " + binding.calendarView.getSelectedCalendar().isCurrentDay());
                LogUtil.showLog("onCalendarSelect -- " + TrunkBranchAnnals.getTrunkBranchYear(calendar.getLunarCalendar().getYear()));
            }
        });
        // 月份改变事件
        binding.calendarView.setOnMonthChangeListener(new CalendarView.OnMonthChangeListener() {
            @Override
            public void onMonthChange(int year, int month) {
                LogUtil.showLog("onMonthChange  -- " + year + "  --  " + month);
                com.haibin.calendarview.Calendar calendar = binding.calendarView.getSelectedCalendar();
                binding.day.setText(String.valueOf(calendar.getYear()) + "年" + calendar.getMonth() + "月");
                mYear = calendar.getYear();
            }
        });
        // 日期长按事件
        binding.calendarView.setOnCalendarLongClickListener(new CalendarView.OnCalendarLongClickListener() {
            @Override
            public void onCalendarLongClickOutOfRange(com.haibin.calendarview.Calendar calendar) {
                LogUtil.showLog("onCalendarLongClickOutOfRange  -- " + String.format("%s : LongClickOutOfRange", calendar));
            }

            @Override
            public void onCalendarLongClick(com.haibin.calendarview.Calendar calendar) {
                LogUtil.showLog("onCalendarLongClick  -- " + "长按不选择日期\n" + getCalendarText(calendar));
            }
        }, true);
        // 周视图切换监听
        binding.calendarView.setOnWeekChangeListener(new CalendarView.OnWeekChangeListener() {
            @Override
            public void onWeekChange(List<com.haibin.calendarview.Calendar> weekCalendars) {
                for (com.haibin.calendarview.Calendar calendar : weekCalendars) {
                    LogUtil.showLog("onWeekChange    " + calendar.toString());
                }
            }
        });
        //
        binding.calendarView.setOnYearViewChangeListener(new CalendarView.OnYearViewChangeListener() {
            @Override
            public void onYearViewChange(boolean isClose) {
                LogUtil.showLog("onYearViewChange  年视图 -- " + (isClose ? "关闭" : "打开"));
                binding.day.setText(mYear + "年");
            }
        });

        //设置日期拦截事件，仅适用单选模式，当前无效
        binding.calendarView.setOnCalendarInterceptListener(new CalendarView.OnCalendarInterceptListener() {
            @Override
            public boolean onCalendarIntercept(com.haibin.calendarview.Calendar calendar) {
                Log.e("onCalendarIntercept", calendar.toString());
                int day = calendar.getDay();
                return day == 1 || day == 3 || day == 6 || day == 11 || day == 12 || day == 15 || day == 20 || day == 26;
            }

            @Override
            public void onCalendarInterceptClick(com.haibin.calendarview.Calendar calendar, boolean isClick) {
                LogUtil.showLog(calendar.toString() + "拦截不可点击");
            }
        });
        // 视图改变事件
        binding.calendarView.setOnViewChangeListener(new CalendarView.OnViewChangeListener() {
            @Override
            public void onViewChange(boolean isMonthView) {
                LogUtil.showLog("onViewChange   ---  " + (isMonthView ? "月视图" : "周视图"));
            }
        });
    }

    private void initData() {

        Map<String, com.haibin.calendarview.Calendar> map = new HashMap<>();
        map.put(getSchemeCalendar(year, month, 3, 0xFFff0000, "假").toString(),
                getSchemeCalendar(year, month, 3, 0xFFff0000, "假"));
        map.put(getSchemeCalendar(year, month, 6, 0xFFe69138, "事").toString(),
                getSchemeCalendar(year, month, 6, 0xFFe69138, "事"));
        map.put(getSchemeCalendar(year, month, 9, 0xFFdf1356, "议").toString(),
                getSchemeCalendar(year, month, 9, 0xFFdf1356, "议"));
        map.put(getSchemeCalendar(year, month, 13, 0xFFedc56d, "记").toString(),
                getSchemeCalendar(year, month, 13, 0xFFedc56d, "记"));
        map.put(getSchemeCalendar(year, month, 14, 0xFFedc56d, "记").toString(),
                getSchemeCalendar(year, month, 14, 0xFFedc56d, "记"));
        map.put(getSchemeCalendar(year, month, 15, 0xFFaacc44, "假").toString(),
                getSchemeCalendar(year, month, 15, 0xFFaacc44, "假"));
        map.put(getSchemeCalendar(year, month, 18, 0xFFbc13f0, "记").toString(),
                getSchemeCalendar(year, month, 18, 0xFFbc13f0, "记"));
        map.put(getSchemeCalendar(year, month, 25, 0xFF13acf0, "假").toString(),
                getSchemeCalendar(year, month, 25, 0xFF13acf0, "假"));
        map.put(getSchemeCalendar(year, month, 27, 0xFF13acf0, "多").toString(),
                getSchemeCalendar(year, month, 27, 0xFF13acf0, "多"));
        //此方法在巨大的数据量上不影响遍历性能，推荐使用
        binding.calendarView.setSchemeDate(map);
    }

    private void loadData() {

        Map<String, com.haibin.calendarview.Calendar> map = new HashMap<>();
        map.put(getSchemeCalendar(year, month, 3, 0xFFff0000, "假").toString(),
                getSchemeCalendar(year, month, 3, 0xFFff0000, "假"));
        map.put(getSchemeCalendar(year, month, 6, 0xFFe69138, "事").toString(),
                getSchemeCalendar(year, month, 6, 0xFFe69138, "事"));
        map.put(getSchemeCalendar(year, month, 9, 0xFFdf1356, "议").toString(),
                getSchemeCalendar(year, month, 9, 0xFFdf1356, "议"));
        map.put(getSchemeCalendar(year, month, 12, 0xFFedc56d, "记").toString(),
                getSchemeCalendar(year, month, 12, 0xFFedc56d, "记"));
        map.put(getSchemeCalendar(year, month, 15, 0xFFaacc44, "假").toString(),
                getSchemeCalendar(year, month, 15, 0xFFaacc44, "假"));
        map.put(getSchemeCalendar(year, month, 18, 0xFFbc13f0, "记").toString(),
                getSchemeCalendar(year, month, 18, 0xFFbc13f0, "记"));
        map.put(getSchemeCalendar(year, month, 21, 0xFF13acf0, "假").toString(),
                getSchemeCalendar(year, month, 21, 0xFF13acf0, "假"));
        map.put(getSchemeCalendar(year, month, 24, 0xFF13acf0, "多").toString(),
                getSchemeCalendar(year, month, 24, 0xFF13acf0, "多"));
        //此方法在巨大的数据量上不影响遍历性能，推荐使用
        binding.calendarView.clearSchemeDate();
        binding.calendarView.setSchemeDate(map);
    }

    private com.haibin.calendarview.Calendar getSchemeCalendar(int year, int month, int day, int color, String text) {
        com.haibin.calendarview.Calendar calendar = new com.haibin.calendarview.Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        calendar.setSchemeColor(color);//如果单独标记颜色、则会使用这个颜色
        calendar.setScheme(text);
        return calendar;
    }

    private static String getCalendarText(com.haibin.calendarview.Calendar calendar) {
        return String.format("新历%s \n 农历%s \n 公历节日：%s \n 农历节日：%s \n 节气：%s \n 是否闰月：%s",
                calendar.getMonth() + "月" + calendar.getDay() + "日",
                calendar.getLunarCalendar().getMonth() + "月" + calendar.getLunarCalendar().getDay() + "日",
                TextUtils.isEmpty(calendar.getGregorianFestival()) ? "无" : calendar.getGregorianFestival(),
                TextUtils.isEmpty(calendar.getTraditionFestival()) ? "无" : calendar.getTraditionFestival(),
                TextUtils.isEmpty(calendar.getSolarTerm()) ? "无" : calendar.getSolarTerm(),
                calendar.getLeapMonth() == 0 ? "否" : String.format("闰%s月", calendar.getLeapMonth()));
    }
}
